<template>
  <div>
    <TopNav title="精彩跟帖" />
    <div class="commentList">
      <Comment
        :commentData="item"
        v-for="(item,index) in commentList"
        :key="index"
        @callReply="callReply"
      />
    </div>
    <CommentInput
      :postId="$route.query.id"
      :parentId="commentId"
      @reloadComment="reloadComment"
      ref="commentInput"
    />
  </div>
</template>

<script>
import TopNav from "@/components/TopNav";
import Comment from "@/components/comment/index";
import CommentInput from "@/components/comment/CommentInput";

export default {
  data() {
    return {
      commentList: [],
      commentId: ""
    };
  },
  components: {
    TopNav,
    Comment,
    CommentInput
  },
  mounted() {
    this.loadPage();
  },
  methods: {
    loadPage() {
      this.$axios({
        url: "/post_comment/" + this.$route.query.id,
        method: "GET"
      }).then(res => {
        console.log(res.data);
        this.commentList = res.data.data;
      });
    },
    callReply(commentId) {
      console.log("触发回复事件,带上评论子组件id,先存放在父组件");
      console.log(commentId);
      this.commentId = commentId;
      //触发输入框子组件
      this.$refs.commentInput.showTextarea();
    },
    reloadComment() {
      this.loadPage();
      //每次发表成功,除了读取评论数据,还要清理之前的commentId回复评论文章的操作
      this.commentId = "";
    }
  }
};
</script>

<style lang='less' scoped>
.commentList {
  padding-bottom: 16.667vw;
}
</style>